#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GeneratedPluginRegistrant registerWithRegistry:self];
  // Override point for customization after application launch.
    
    FlutterViewController *vc = (FlutterViewController *)self.window.rootViewController;
    
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"felixyin/battery" binaryMessenger:vc.binaryMessenger];
    
    //设置方法监听
    __weak typeof(self) weakSelf = self;
    [channel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        if([call.method  isEqual: @"getBatteryInfo"]){
            UIDevice *device = [UIDevice currentDevice];
            [device setBatteryMonitoringEnabled:YES];
            if (device.batteryState == UIDeviceBatteryStateUnknown) {
                //没有获取到
//                result([FlutterError errorWithCode:@"unknown" message:@"Battery is unknown" details:nil]);
                result(@(1000));
                return;
            }
            //返回电池电量
            result(@(device.batteryLevel));
        }else if([call.method isEqualToString:@"test"]){
            [weakSelf flutterCallNative:call.arguments result:result];
        }else{
            //没有获取到
            result([FlutterError errorWithCode:@"unknown" message:@"Battery is unknown" details:nil]);
        }
    }];
    
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (void) flutterCallNative:(id) param result:(FlutterResult) result{
    NSLog(@"%@",param);
    result(@"原生回传参数1");
}

@end
